package com.nf.action;

import com.nf.entity.Account;
import com.nf.entity.Record;
import com.nf.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;


@Controller
@SessionAttributes("account")
public class AccountAction {

    @Autowired
    private AccountService accountService;


    @ModelAttribute("account")
    Account getAccount() {
        return null;
    }

    //0.首页
    @RequestMapping("/index")
    public String index(@ModelAttribute("account") Account account,Model model){

        System.out.println(account);
        if(account==null){
            model.addAttribute("message","LOGIN_TIME_OUT");
            return "redirect:loginView";
        }
        return "index";
    }

    //这是登陆页面
    @RequestMapping("/loginView")
    public String loginView(){
        return "login";
    }

    //登录时检查的业务逻辑
    @RequestMapping("/loginCheck")
    public String loginCheck(String userName,String password,Model model){
        System.out.println(userName+"  "+password);
        //判断用户是否登陆成功
        Account account = accountService.login(userName, password);
        if (account==null){
            //return "redirect:loginView?message=login_error";
            //等同
            model.addAttribute("message","LOGIN_ERROR");
            return "redirect:loginView";

        }
        model.addAttribute("account",account);
        //存放session
        return "redirect:index";
    }

    //2.查询余额
    @RequestMapping("/queryBanlance")
    public String queryBanlance(@ModelAttribute("account") Account account,Model model){

        if (account==null){
            //return "redirect:loginView?message=login_error";
            //等同
            model.addAttribute("message","LOGIN_ERROR");
            return "redirect:loginView";

        }


        Integer banlance = accountService.queryBanlanceById(account.getId());
        model.addAttribute("banlance",banlance);
        return "banlance";
    }

    //3.1 修改密码的页面
    @RequestMapping("/updatePasswordView")
    public String updatePasswordView(@ModelAttribute("account") Account account,Model model){
        if (account==null){
            //return "redirect:loginView?message=login_error";
            //等同
            model.addAttribute("message","LOGIN_ERROR");
            return "redirect:loginView";

        }
        return "updatePasswordView";
    }


    //修改密码的action
    @RequestMapping("/updatePassword")
    public String updatePassword(@ModelAttribute("account") Account account,Model model,String newPassword,
                                 String oldPassword,String newPassword2){
        if (account==null){
            //return "redirect:loginView?message=login_error";
            //等同
            model.addAttribute("message","LOGIN_ERROR");
            return "redirect:loginView";

        }
        //校验（属于控制器的校验，不是service的校验）
        if (newPassword.equals(oldPassword)){
            model.addAttribute("message","OLD_EQUAL_NEW");
            return "redirect:updatePasswordView";
        }

        if (newPassword.length()>6){
            model.addAttribute("message","PASSWORD_LONG");
            return "redirect:updatePasswordView";
        }
        if (!newPassword.equals(newPassword2)){
            model.addAttribute("message","P1_NOT_EQUAL_P2");
            return "redirect:updatePasswordView";
        }
        boolean flag = accountService.updatePassword(newPassword, oldPassword, account.getId());
        if(flag) {
            return "updatePassword_success";
        }else{
            return "updatePassword_error";
        }
    }

}
